#include "bbox.h"

#define max(a, b) ((a>b)?a:b)
#define min(a, b) ((a<b)?a:b)


bbox::bbox()
{
  mLeft=mTop=mRight=mBottom = 0;
}
bbox::bbox(int left, int top, int right, int bottom)
{
  mLeft = left; mTop = top;
  mRight = right; mBottom = bottom;
}
bbox bbox::intersection(bbox a, bbox b)
{
  bbox c;
  c.mLeft = max(a.mLeft, b.mLeft);
  c.mTop = max(a.mTop, b.mTop);
  c.mRight = min(a.mRight, b.mRight);
  c.mBottom = min(a.mBottom, b.mBottom);
  return c;
}
bool bbox::inside(v2<int> pos)
{
  return (pos.x>=mLeft&&pos.y>=mTop&&pos.x<mRight&&pos.y<mBottom);
}
int bbox::left() const { return mLeft; }
int bbox::top() const { return mTop; }
int bbox::right() const { return mRight; }
int bbox::bottom() const { return mBottom; }
int bbox::width() const { return mRight-mLeft; }
int bbox::height() const { return mBottom-mTop; }
int bbox::area() const
{
  int w = mRight-mLeft;
  int h = mBottom-mTop;
  if(w<0&&h<0) h *= -1;
  return w*h;
}
void bbox::move(int dx, int dy)
{
  mLeft += dx; mRight += dx;
  mTop += dy; mBottom += dy;
}
bool bbox::operator<(bbox a) { return area()<a.area(); }
bool bbox::operator<(int a) { return area()<a; }
bool bbox::operator<=(bbox a) { return area()<=a.area(); }
bool bbox::operator<=(int a) { return area()<=a; }
bool bbox::operator>(bbox a) { return area()>a.area(); }
bool bbox::operator>(int a) { return area()>a; }
bool bbox::operator>=(bbox a) { return area()>=a.area(); }
bool bbox::operator>=(int a) { return area()>=a; }
bool bbox::operator!=(bbox a) { return area()!=a.area(); }
bool bbox::operator!=(int a) { return area()!=a; }
bool bbox::operator==(bbox a) { return area()==a.area(); }
bool bbox::operator==(int a) { return area()==a; }
